home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Publishing / ImagePortfolio / Source / ExpandedView.m < prev    next >
Text File  |  1994-04-01  |  5KB  |  165 lines

  1. // -------------------------------------------------------------------------------------
  2. // ExpandedView.m
  3. // Martin D. Flynn, NeXT Computer, Inc.
  4. // You may freely copy, distribute and reuse the code in this example.
  5. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  6. // fitness for any particular use.
  7. // -------------------------------------------------------------------------------------
  8.  
  9. #import <stdlib.h>
  10. #import <stdio.h>
  11. #import <string.h>
  12. #import <math.h>
  13. #import <libc.h>
  14. #import <sys/time.h>
  15. #import <defaults/defaults.h>
  16. #import <appkit/graphics.h>
  17. #import <appkit/Panel.h>
  18. #import <appkit/NXImage.h>
  19. #import <appkit/NXBitmapImageRep.h>
  20. #import <appkit/NXEPSImageRep.h>
  21. #import <appkit/ScrollView.h>
  22. #import <dpsclient/psops.h>
  23. #import <dpsclient/dpsNeXT.h>
  24. #import <objc/objc-runtime.h>
  25. #import "ExpandedView.h"
  26.  
  27. // -------------------------------------------------------------------------------------
  28. #define    minWIDTH    48.0
  29. #define    minHEIGHT    48.0
  30.  
  31. @implementation ExpandedView
  32.  
  33. // -------------------------------------------------------------------------------------
  34. // create a new instance
  35.  
  36. /* standard init */
  37. - initFrame:(const NXRect *)frameRect
  38. {
  39.     [super initFrame:frameRect];
  40.     imageId = (id)nil;
  41.     return self;
  42. }
  43.  
  44. /* init from scroll view */
  45. - setScrollView:scrollView
  46. {
  47.     myScrollView = scrollView;
  48.     return self;
  49. }
  50.  
  51. // -------------------------------------------------------------------------------------
  52. // image methods
  53.  
  54. /* set image */
  55. - setImage:image
  56. {
  57.     NXRect    vFrame;
  58.     id        rep;
  59.   
  60.     /* check for valid image representation */
  61.     if (!(imageId=(image&&[[image representationList] count])?image:(id)nil)) return self;
  62.   
  63.     /* resize view to match image */
  64.     [imageId getSize:&imageFrame.size];
  65.     vFrame.origin.x = vFrame.origin.y = 0.0;
  66.     vFrame.size = imageFrame.size;
  67.     if (vFrame.size.width  < minWIDTH ) vFrame.size.width  = minWIDTH ;
  68.     if (vFrame.size.height < minHEIGHT) vFrame.size.height = minHEIGHT;
  69.     [self setFrame:&vFrame];
  70.     drawWithAlpha = YES;
  71.   
  72.     /* center image in view */
  73.     imageFrame.origin.x = (bounds.size.width  - imageFrame.size.width ) / 2.0;
  74.     imageFrame.origin.y = (bounds.size.height - imageFrame.size.height) / 2.0;
  75.   
  76.     /* set defaqult background gray value */
  77.     imageRep = 0;
  78.     rep = [[imageId representationList] objectAt:imageRep];
  79.     backgroundGray = ([rep isKindOf:[NXEPSImageRep class]])? NX_WHITE : NX_LTGRAY;
  80.  
  81.     return self;
  82. }
  83.  
  84. // -------------------------------------------------------------------------------------
  85. // mouse down
  86.  
  87. - mouseDown:(NXEvent*)e
  88. {
  89.     if (!imageId) return self;
  90.     if ((e->flags) & NX_ALTERNATEMASK) {
  91.         if (drawWithAlpha) { imageRep = 0; drawWithAlpha = NO; }
  92.         else imageRep = (++imageRep) % [[imageId representationList] count];
  93.     } else {
  94.         if (drawWithAlpha) return self;
  95.         drawWithAlpha = YES;
  96.     }
  97.     [self display];
  98.     return self;
  99. }
  100.  
  101. // -------------------------------------------------------------------------------------
  102. // draw methods
  103.  
  104. /* draw self */
  105. - drawSelf:(const NXRect *)r :(int)c
  106. {
  107.     PSsetgray((imageId)?backgroundGray:NX_LTGRAY);
  108.     NXRectFill(&bounds);
  109.     if (imageId) {
  110.         if (drawWithAlpha) [imageId composite:NX_SOVER toPoint:&imageFrame.origin];
  111.         else {
  112.             id repList = [imageId representationList];
  113.             id rep = [repList objectAt:imageRep % [repList count]];
  114.             [rep drawIn:&imageFrame];
  115.         }
  116.     }
  117.     return self;
  118. }
  119.  
  120. // -------------------------------------------------------------------------------------
  121. // expanded image window delegate methods
  122.  
  123. /* window is resizing */
  124. - windowWillResize:windowId toSize:(NXSize*)newSize
  125. {
  126.     NXRect    fRect, cRect;
  127.     NXSize    cSize = imageFrame.size;
  128.   
  129.     /* get scroll frame size */
  130.     cSize.width  = MAX(imageFrame.size.width , minWIDTH );
  131.     cSize.height = MAX(imageFrame.size.height, minHEIGHT);
  132.     [ScrollView getFrameSize:&cRect.size forContentSize:&cSize
  133.                horizScroller:YES vertScroller:YES borderType:[myScrollView borderType]];
  134.  
  135.     /* get window frame size */
  136.     cRect.origin.x = cRect.origin.y = 0.0;
  137.     [Window getFrameRect:&fRect forContentRect:&cRect style:[windowId style]];
  138.  
  139.     /* limit size */
  140.     if (newSize->width  > fRect.size.width ) newSize->width  = fRect.size.width ;
  141.     if (newSize->height > fRect.size.height) newSize->height = fRect.size.height;
  142.  
  143.     return self;
  144. }
  145.  
  146. /* window is resizing */
  147. - windowDidResize:windowId
  148. {
  149.     NXRect    fRect, cRect;
  150.   
  151.     /* get window content sizer */
  152.     [windowId getFrame:&fRect];
  153.     [Window getContentRect:&cRect forFrameRect:&fRect style:[windowId style]];
  154.   
  155.     /* set scroller */
  156.     cRect.origin.x = cRect.origin.y = 0.0;
  157.     [myScrollView setFrame:&cRect];
  158.     [myScrollView setHorizScrollerRequired:YES];
  159.     [myScrollView setVertScrollerRequired:YES];
  160.   
  161.     return self;
  162. }
  163.  
  164. @end
  165.